﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Himp.Outpatient
{
    /// <summary>
    /// 门诊处方订单
    /// </summary>
    public class OtpRxOrdService : ApplicationService, IOtpRxOrdService
    {
        private OtpRxManger _otpRxManger;
        private IOtpOrdRecService _otpOrdRecService;
        private IRepository<OtpRxOrd, Guid> _otpRxOrdRepository;

        public OtpRxOrdService(OtpRxManger otpRxManger, IOtpOrdRecService otpOrdRecService
            , IRepository<OtpRxOrd, Guid> otpRxOrdRepository)
        {
            _otpRxManger = otpRxManger;
            _otpOrdRecService = otpOrdRecService;
            _otpRxOrdRepository = otpRxOrdRepository;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<OtpRxOrdDto>> GetListAsync(OtpRxOrdInput input)
        {
            var iQuery = await _otpRxOrdRepository.GetQueryableAsync();
            iQuery = iQuery.WhereIf(string.IsNullOrEmpty(input.Invono), i => i.Invono == input.Invono);

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<OtpRxOrd>, List<OtpRxOrdDto>>(iQuery.ToList());

                return new PagedResultDto<OtpRxOrdDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<OtpRxOrd>, List<OtpRxOrdDto>>(items.ToList());

                return new PagedResultDto<OtpRxOrdDto>(totalCount, itemsDto);
            }
        }

        /// <summary>
        /// 创建门诊处方支付记录
        /// </summary>
        /// <returns></returns>
        public async Task<OtpOrdRecDto> PostAsync(OtpRxOrdCreateDto input)
        {
            var otpRxRecDetls = await _otpRxManger.GetOtpNotPayRxRecDetlList(input.OtpRxOrdIds);
            OtpOrdRecCreateDto otpOrdRecCreateDto =new OtpOrdRecCreateDto();
            otpOrdRecCreateDto.OrdBizSouc = EnumOrdBizSouc.门诊处方;
            otpOrdRecCreateDto.SetlTypeCode = input.SetlTypeCode;
            otpOrdRecCreateDto.SetlTypeName = input.SetlTypeName;
            otpOrdRecCreateDto.TotAmt = otpRxRecDetls.Sum(x => x.TotAmt);
            otpOrdRecCreateDto.DiscAmt = 0;
            otpOrdRecCreateDto.AdvAmt = 0;
            otpOrdRecCreateDto.RecAmt = otpOrdRecCreateDto.TotAmt;
            otpOrdRecCreateDto.SelfpayAmt = otpOrdRecCreateDto.TotAmt;
            var otpOrdRec = await _otpOrdRecService.PostAsync(otpOrdRecCreateDto);

            //保存门诊处方关联订单
            OtpRxOrd otpRxOrd = new OtpRxOrd();
            var otpRxOrds = input.OtpRxOrdIds.Select(x => new OtpRxOrd()
            {
                OtpRxRecDetlId = x,
                OtpOrdRecId = otpOrdRec.Id,
                OrdNo = otpOrdRec.OrdNo,
                Invono = otpOrdRec.Invono
            });

            await _otpRxOrdRepository.InsertManyAsync(otpRxOrds);

            return otpOrdRec;
        }

        /// <summary>
        /// 创建门诊处方退款记录
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<OtpOrdRecDto> PostRefundAsync(OtpRxOrdCreateDto input)
        {
            var otpRxRecDetls = await _otpRxManger.GetOtpNotPayRxRecDetlList(input.OtpRxOrdIds);
            OtpOrdRecCreateDto otpOrdRecCreateDto = new OtpOrdRecCreateDto();
            otpOrdRecCreateDto.OrdBizSouc = EnumOrdBizSouc.门诊处方退款;
            otpOrdRecCreateDto.SetlTypeCode = "09";
            otpOrdRecCreateDto.SetlTypeName = "原路退回";
            otpOrdRecCreateDto.TotAmt = - otpRxRecDetls.Sum(x => x.TotAmt);
            otpOrdRecCreateDto.DiscAmt = 0;
            otpOrdRecCreateDto.AdvAmt = 0;
            otpOrdRecCreateDto.RecAmt = 0;
            otpOrdRecCreateDto.SelfpayAmt = 0;
            var otpOrdRec = await _otpOrdRecService.PostAsync(otpOrdRecCreateDto);

            //保存门诊处方关联订单
            OtpRxOrd otpRxOrd = new OtpRxOrd();
            var otpRxOrds = input.OtpRxOrdIds.Select(x => new OtpRxOrd()
            {
                OtpRxRecDetlId = x,
                OtpOrdRecId = otpOrdRec.Id,
                OrdNo = otpOrdRec.OrdNo,
                Invono = otpOrdRec.Invono
            });

            await _otpRxOrdRepository.InsertManyAsync(otpRxOrds);

            return otpOrdRec;
        }
    }
}
